<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
</style>
<template>
  <div class="app-container">
    <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit"
                 >导入文件
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleAdd">新增
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" min-width="100" label="卡BIN" prop="cardBin"/>        
      <el-table-column align="center" min-width="100" label="卡长度" prop="binLen"/>
      <el-table-column align="center" min-width="100" label="银行代码" prop="bankCode"/>
      <el-table-column align="center" min-width="100" label="银行名称" prop="bankName"/>
      <el-table-column align="center" min-width="100" label="卡类型" prop="cardType"/>
      <el-table-column align="center" min-width="100" label="卡属性" prop="cardCategory"/>
      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button type="primary" size="mini"
                     @click="handleEdit(scope.row)">修改
          </el-button>
          <el-button type="danger" size="mini"
                     @click="doDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
    @pagination="getList"/>
    <!-- 分配订单对话框 -->
    <el-dialog :visible.sync="editDialogVisible" title="新增银行卡">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="卡BIN" prop="cardBin">
          <el-input v-model="editForm.cardBin" placeholder="卡BIN"/>
        </el-form-item>
        <el-form-item label="卡长度" prop="binLen">
          <el-input v-model="editForm.binLen" auto-complete="off" placeholder="卡名称"/>
        </el-form-item>
        <el-form-item label="银行代码" prop="bankCode">
          <el-input v-model="editForm.bankCode" placeholder="银行代码"/>
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input v-model="editForm.bankName" placeholder="银行名称"/>
        </el-form-item>
        <el-form-item label="卡类型" prop="cardType">
          <el-radio v-model="editForm.cardType" label="0">借记卡</el-radio>
          <el-radio v-model="editForm.cardType" label="1">信用卡</el-radio>
        </el-form-item>
        <el-form-item label="卡属性" prop="cardCategory">
          <el-input v-model="editForm.cardCategory"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doEdit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { doGet, doPost } from '@/api'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import checkPermission from '@/utils/permission' // 权限判断函数

  export default {
    name: 'bank',
    components: { Pagination },
    filters: {
        // accTyped: function(val){
        //     return function (val){
        //         return {0: "服务费账户",1: "代付账户"}[val] || "未知"
        //     }
        // },
    },
    data() {
      return {
        list: [],
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
        },
        editForm: {
          cardBin: undefined,
          binLen: undefined,
          bankCode: undefined,
          bankCode: undefined,
          bankName: undefined,
          cardType: undefined,
          cardCategory: undefined
        },
        editFormRules: {
          cardBin: [{ required: true, message: '卡BIN不允许为空', trigger: 'blur' }],
          binLen: [{ required: true, message: '卡名称不允许为空', trigger: 'blur' }],
          bankCode: [{ required: true, message: '银行代码不允许为空', trigger: 'blur' }],
          bankName: [{ required: true, message: '银行名称不允许为空', trigger: 'blur' }],
          cardCategory: [{ required: true, message: '卡属性不允许为空', trigger: 'blur' }]
        },
        editDialogVisible: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      checkPermission,
      getList() {
        this.listLoading = true
        doGet('cardbin.html?op=list', this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.rows.length
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      handleAdd() {
        this.editForm.bankName = undefined
        this.editForm.bankCode = undefined
        this.editDialogVisible = true
        this.dialogStatus = "create"
        this.editDialogVisible = true
        this.$nextTick(() => {
            this.$refs['dataForm'].clearValidate()
        })
      },
      handleEdit(row) {
            this.editForm = Object.assign({}, row);
            this.editIndex=this.list.indexOf(row);
            this.dialogStatus = "update"
            this.editDialogVisible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].clearValidate()
            })
        },
      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('cardbin.html?op=add', this.editForm)
              .then(response => {
                this.getList()
                this.editDialogVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加银行编码成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      doDelete(row) {
        doGet("cardbin.html?op=del",row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除银行成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      }
    }
  }
</script>
